package com.yuwj.chinaa.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.activity.AccountSettingActivity;
import com.yuwj.chinaa.activity.AssetWalletActivity;
import com.yuwj.chinaa.activity.ContactUsActivity;
import com.yuwj.chinaa.activity.ExerciseAddActivity;
import com.yuwj.chinaa.activity.ExerciseOrderListActivity;
import com.yuwj.chinaa.activity.FeedbackActivity;
import com.yuwj.chinaa.activity.MainActivity;
import com.yuwj.chinaa.activity.MePromotionActivity;
import com.yuwj.chinaa.activity.MyCommunityActivity;
import com.yuwj.chinaa.activity.NewsListActivity;
import com.yuwj.chinaa.activity.NoticeListActivity;
import com.yuwj.chinaa.activity.PartnerApplyActivity;
import com.yuwj.chinaa.activity.ReceiveAddressListActivity;
import com.yuwj.chinaa.activity.ShareGeneralizeActivity;
import com.yuwj.chinaa.activity.UserFeedbackActivity;
import com.yuwj.chinaa.base.BaseLazyLoadFragment;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.entity.EntityUser;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ActivityCollectUtil;
import com.yuwj.chinaa.util.SharedpreferenceUtil;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

public class FragmentMe extends BaseLazyLoadFragment {
    EntityUser entityUser;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_user_photo)
    CircleImageView ivUserPhoto;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_verify_state)
    TextView tvVerifyState;
    Unbinder unbinder;
    IPresenter presenter;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.fragment_me;
    }

    public static FragmentMe createInstance() {
        FragmentMe fragmentMe = new FragmentMe();
        Bundle bundle = new Bundle();
        fragmentMe.setArguments(bundle);
        return fragmentMe;
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        initParam();
        initView();
        initOperator();
    }


    public void initParam() {
        presenter = new PresenterImpl(this);
        entityUser = SharedpreferenceUtil.getInstance().getUserInfo(context);
    }

    public void initView() {
        tvTitle.setText("我的");
        Glide.with(context).load(entityUser.getPic()).error(R.mipmap.ic_asset_user_photo).into(ivUserPhoto);
        tvLevel.setText(entityUser.getUserTypeName());
        tvUsername.setText(entityUser.getName());
        tvId.setText(entityUser.getUserId());
        String VerifyState = "";
        switch (entityUser.getIsIdentification()) {
            case ConstantParam.GENERAL_AUTH_WITH_FAIL:
                VerifyState = "认证拒绝";
                break;
            case ConstantParam.GENERAL_AUTH_WITH_NO:
                VerifyState = "未提交认证";
                break;
            case ConstantParam.GENERAL_AUTH_WITH_ING:
                VerifyState = "等待认证";
                break;
            case ConstantParam.GENERAL_AUTH_WITH_SUCCESS:
                VerifyState = "已认证";
                break;
        }
        tvVerifyState.setText(VerifyState);
    }


    public void initOperator() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (context != null) {
            entityUser = SharedpreferenceUtil.getInstance().getUserInfo(context);
            initView();
        }
    }

    @OnClick({R.id.iv_setting, R.id.ll_partner_apply, R.id.ll_share_container, R.id.ll_exercise_center_container, R.id.ll_exercise_order_container
            , R.id.ll_shipping_address_container, R.id.ll_message_container, R.id.ll_lianx_container, R.id.ll_feedback_container, R.id.ll_quit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_setting:
                startActivity(AccountSettingActivity.createIntent(context));
                break;
            case R.id.ll_partner_apply:
                startActivity(PartnerApplyActivity.createIntent(context));
                break;
            case R.id.ll_share_container:
                startActivity(ShareGeneralizeActivity.createIntent(context));
                break;
            case R.id.ll_exercise_center_container:
                startActivity(ExerciseAddActivity.createIntent(context));
                break;
            case R.id.ll_exercise_order_container:
                startActivity(ExerciseOrderListActivity.createIntent(context));
                break;
            case R.id.ll_shipping_address_container:
                startActivity(ReceiveAddressListActivity.createIntent(context));
                break;
            case R.id.ll_message_container:
                startActivity(NoticeListActivity.createIntent(context));
                break;
            case R.id.ll_lianx_container:
                startActivity(ContactUsActivity.createIntent(context));
                break;
            case R.id.ll_feedback_container:
                startActivity(UserFeedbackActivity.createIntent(context));
                break;
            case R.id.ll_quit:
                JSONObject paramWithLogout = new JSONObject();
                try {
                    paramWithLogout.put("uphone", entityUser.getUserId());
                    paramWithLogout.put("uid", entityUser.getID() + "");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.userLogout(paramWithLogout.toString());
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onUserLogoutSuccess() {
        super.onUserLogoutSuccess();
        // 退出程序
        //清除用户信息,清除header里面的token
        SharedpreferenceUtil.getInstance().clearUserInfo(context);
        OkGo.getInstance().getCommonHeaders().remove("token");
        ActivityCollectUtil.getInstance().finishAll();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    @Override
    public void onUserLogoutFail(Exception e) {
        super.onUserLoginFail(e);
        // 退出程序
        //清除用户信息,清除header里面的token
        SharedpreferenceUtil.getInstance().clearUserInfo(context);
        OkGo.getInstance().getCommonHeaders().remove("token");
        ActivityCollectUtil.getInstance().finishAll();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

}
